<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


/**
 *  File : goContent.php
 *  called only via ajax 
 *  Opens access to content for the student
 *   
 */  
 

checkSession();

require '../../includes/classes/WS3tableview.php';
require '../../includes/classes/WS3user.php';
require '../../includes/classes/WS3course.php';
require '../../includes/classes/WS3lesson.php';
require '../../includes/classes/WS3module.php';
require '../../includes/classes/WS3content.php';

$date=date("Y-m-d H:i:s");
$user = new WS3_User(); 

if ($user->setUserID( $session->userID() ) )
{

  $id=$_POST['id']; // content id

  $content = new WS3_Content();
  $content->setContentID( $_POST['id'] );
  
  $module = new WS3_Module();
  $module->setModuleID( $content->ModuleID() ); 
  
  $lesson = new WS3_Lesson();
  $lesson->setLessonID( $_POST['lid'] );
  
  $course = new WS3_Course();
  $course->setCourseID( $lesson->CourseID() );  
  
  $display =" Displaying content ";
  // Tracking
  // update user_access
  //$$TODO check if the access to this content is once_only
  //$$TODO if not once only, update or insert
 $q = "INSERT INTO user_history (
  `id`,
  `course_id`,
  `lesson_id`,
  `module_id`,
  `content_id`,
  `date_access`,
  `date_modified`,
  `counter`,
   user_id
   ) 
   VALUES
   (NULL,
   '".$course->CourseID()."',
   '".$lesson->LessonID()."',
   '".$module->ModuleID()."',
   '".$content->ContentID()."',
   '$date',
   '$date',
   (counter+1),
   '".$user->ID()."'
   )";
   
   
  $db=new WS3_Database();
  if ($db->query($q))
  {
    $display .=" <br />Added tracking ";
  }

   //$$REMOVE this is just to help develope the scoring
 $q = "INSERT INTO user_score (
  `id`,
  `course_id`,
  `lesson_id`,
  `module_id`,
  `content_id`,
  `date_created`,
  `date_modified`,
  `score`,
   user_id
   ) 
   VALUES
   (NULL,
   '".$course->CourseID()."',
   '".$lesson->LessonID()."',
   '".$module->ModuleID()."',
   '".$content->ContentID()."',
   '$date',
   '$date',
   '23',
   '".$user->ID()."'
   )";
   
   
  $db=new WS3_Database();
  if ($db->query($q))
  {
    $display .=" <br />Added score ";
  } 
 
}

$message = "Access to content";


/** Get menu */
require '../../includes/menus/topGoContent.php';
$menu=javaSafeString($menu);
echo "document.getElementById('topmenu').innerHTML='$menu';";

echo "document.getElementById('main').innerHTML = '".$display ."';";

echo "document.getElementById('messages').innerHTML='$message';";

if (W3DEBUG)  $debug=new WS3_Debug();

?>
